package com.multiplayer.service.impl;


import java.util.List;

import org.json.JSONObject;

import com.multiplayer.dao.IUserDao;
import com.multiplayer.dao.impl.UserDaoImpl;
import com.multiplayer.entity.Reservation;
import com.multiplayer.entity.Student;
import com.multiplayer.entity.User;
import com.multiplayer.service.IUserService;
import com.multiplayer.tools.FaceRecognition;
import com.multiplayer.tools.PageData;

/**
 * 员工登录注册Service
 * @author zhangwenhao 2020.5.11
 *
 */
public class UserServiceImpl implements IUserService {
	IUserDao userDao = new UserDaoImpl();
	@Override
	public boolean userLogin(String username, String upassword) {
		boolean flag = false;
		List<User> list = userDao.queryUserById(username);
		User user = null;
		if(list.size()>0) {
			for(User u:list) {
				user = u;
			}
			
			if(upassword.equals(user.getUpassword())) {
				//登录成功返回true
				flag = true;
			}else {
				//登录失败返回false
				flag = false;
			}
		}else {
			//用户名不存在直接返回false
			return false;
		}
		return flag;
	}

	@Override
	public boolean userRegist(String username, String upassword) {
		//1.查找学号是否在学生表中存在
		List<Student> list  = userDao.queryStudent(username);
		//2.判断是否已经存在这个学号的用户
		List<User> list1 = userDao.queryUserById(username);
		
		boolean flag = false;
		if(list.size()>0 && list1.size() == 0) {
			User user = new User(username, username, upassword, 100.0);
			flag = userDao.saveUser(user);
		}else {
			return false;
		}
		return flag;
	}

	
	@Override
	public JSONObject userLoginByFace(String image, String imageType,String groupIdList) {
		//[{"score":92.29216003418,"group_id":"User","user_id":"E001","user_info":""}]
		JSONObject res = FaceRecognition.searchFace(image, imageType, groupIdList);
		return res;
	}

	@Override
	public PageData searchAllUserByPage(Integer page, Integer pageSize) {
		PageData pageData = userDao.queryUserPageDate(page, pageSize);
		return pageData;
	}



	

}
